package com.junzhiit.nengji.api.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bainaeco.framework.util.DateUtil;
import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.api.service.QuestionService;
import com.junzhiit.nengji.api.web.exception.QuestionNotFoundException;
import com.junzhiit.nengji.api.web.exception.UserNotFoundException;
import com.junzhiit.nengji.api.web.vo.QuestionInfoVO;
import com.junzhiit.nengji.api.web.vo.QuestionTreeVO;
import com.junzhiit.nengji.api.web.vo.QuestionUseVO;
import com.junzhiit.nengji.api.web.vo.QuestionVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.bo.QuestionBO;
import com.junzhiit.nengji.dao.QuestionDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.cache.QuestionCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Question;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.enums.QuestionUseStatus;

@Service
public class QuestionServiceImpl extends BaseServiceImpl implements QuestionService {
	
	@Autowired
	private QuestionDAO questionDAO;
	
	@Autowired
	private QuestionCacheDAO questionCacheDAO;
	
	@Autowired
	private UserDAO userDAO;

	@Override
	public ResultVO findAll() {
		ResultVO result=new ResultVO();
		List<QuestionBO> list=questionDAO.findAlls();
		
		Map<String, QuestionTreeVO> map = new LinkedHashMap<String, QuestionTreeVO>();
		for (QuestionBO questionBO : list) {
			QuestionTreeVO questionTreeVO=map.get(questionBO.getQuestionTypeId());
			if (questionTreeVO==null) {
				questionTreeVO = new QuestionTreeVO();
				BeanUtils.copyProperties(questionBO, questionTreeVO);
	        	List<QuestionVO> questionVOList = new ArrayList<QuestionVO>();
	        	questionTreeVO.setList(questionVOList);
	            map.put(questionTreeVO.getQuestionTypeId(),questionTreeVO);
	        }
	        
			QuestionVO questionVO=new QuestionVO();
			BeanUtils.copyProperties(questionBO, questionVO);
			questionTreeVO.getList().add(questionVO);
		}
		
		Collection<QuestionTreeVO> data = map.values();
		
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO findQuestion(QuestionUseVO questionUseVO) {
		ResultVO result=new ResultVO();
		Question question= questionDAO.query(questionUseVO.getQuestionId());
		if(question==null){
			throw new QuestionNotFoundException("问题没找到!");
		}
		
		User user=userDAO.query(questionUseVO.getUserId());
		if(user==null){
			throw new UserNotFoundException("用户没找到!");
		}
		
		QuestionInfoVO questionInfoVO=new QuestionInfoVO();
		BeanUtils.copyProperties(question, questionInfoVO);
		questionInfoVO.setHas(false);
		
		String key= DIYKey.getQuestionKey(DateUtil.getCurrentDateStr(DateUtil.DATE_PATTERN_YYYYMMDD));
		String str=questionCacheDAO.getString(key);
		
		if(StringUtils.isNotBlank(str)){
			List<QuestionUseVO> list=JSON.parseArray(str, QuestionUseVO.class);
			for (QuestionUseVO param : list) {
				if(param.getQuestionId().equals(questionUseVO.getQuestionId())&&param.getUserId().equals(questionUseVO.getUserId())){
					questionInfoVO.setHas(true);
					break;
				}
			}
		}
	
		result.setData(questionInfoVO);
		return result;
	}

	@Override
	public ResultVO edit(QuestionUseVO questionUseVO) {
		ResultVO result=new ResultVO();
		Question question= questionDAO.query(questionUseVO.getQuestionId());
		if(question==null){
			throw new QuestionNotFoundException("问题没找到!");
		}
		
		User user=userDAO.query(questionUseVO.getUserId());
		if(user==null){
			throw new UserNotFoundException("用户没找到!");
		}
		
		String key= DIYKey.getQuestionKey(DateUtil.getCurrentDateStr(DateUtil.DATE_PATTERN_YYYYMMDD));
		String str=questionCacheDAO.getString(key);
		
		List<QuestionUseVO> list=new ArrayList<QuestionUseVO>();
		if(StringUtils.isNotBlank(str)){
			list=JSON.parseArray(str, QuestionUseVO.class);
			for (QuestionUseVO param : list) {
				if(param.getQuestionId().equals(questionUseVO.getQuestionId())&&param.getUserId().equals(questionUseVO.getUserId())){
					result.setMessage("您今天已经对该回复顶或踩过！");
					return result;
				}
			}
			list.add(questionUseVO);
		}else{
			list.add(questionUseVO);
		}
	
		questionCacheDAO.addString(key, JsonUtil.toJson(list));
		
		if(questionUseVO.getStatus()==QuestionUseStatus.ACTIVE.toIntStatus()){
			question.setUseful(question.getUseful()+1);
		}else if(questionUseVO.getStatus()==QuestionUseStatus.INACTIVE.toIntStatus()){
			question.setUseless(question.getUseless()+1);
		}
		question.setUpdatedTime(new Date());
		
		result.setData(questionDAO.update(question));
		return result;
	}

}
